<?php
/*
 * 评价
 */

namespace app\adminbackstage\controller;
use app\BaseController;
use think\facade\Request;
use app\adminbackstage\model\Evaluation;
use app\adminbackstage\model\User;

class EvaluationController extends BaseController{

    //评价列表
    public function evaluationList(){
        $a_uid = Request::param('a_uid');
        $page = Request::param('page');
        $num = Request::param('num');
        
//        $express_name = Request::param('express_name');
        $p_name = Request::param('p_name');
        $phone = Request::param('phone');
        $username = Request::param('username');
        $o_pro_num = Request::param('o_pro_num');
        $p_num = Request::param('p_num');
        
        $state = Request::param('state');
        $paytime_min = Request::param('paytime_min');
        $paytime_max = Request::param('paytime_max');

        $where = [];
        $where[] = ['e.enable','=','1'];
        if(!empty($express_name)){
            $where[] = ['e.p_name','like','%'.$p_name.'%'];
        }
        
        if(!empty($state)){
            if($state==-1){
                $state=0;
            }
            $where[] = ['e.state','=',$state];
        }
        
        if(!empty($paytime_min) && !empty($paytime_max)){
            
            $paytime_min = strtotime($paytime_min);
            $paytime_max = strtotime($paytime_max);
            $where[] = ['o.paytime','>=',$paytime_min];
            $where[] = ['o.paytime','<=',$paytime_max];
        }
        
        if(!empty($phone)){
            $where[] = ['u.phone','=',$phone];
        }
        
        if(!empty($username)){
            $where[] = ['u.username','=',$username];
        }
        if(!empty($o_pro_num)){
           $where[] = ['o.o_pro_num','=',$o_pro_num];
        }
        if(!empty($p_num)){
           $where[] = ['a.p_num','=',$p_num];
        }
        $list = Evaluation::evaluationList($where,$page,$num);
        if(!empty($list)){
            $list = $list->toArray();
            foreach ($list as $key => $value) {
                $list[$key]["addtime"] = date( "Y-m-d H:i:s",$value["addtime"]);
                
                    /*if(strpos($value['img'],"img.alicdn.com") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    elseif(strpos($value['img'],"http") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    else{
                        
                        $storeList[$key]["img"] =  Config::get('app.swimming_url').$value['img'];
                    }*/
            }
        }
        $count = Evaluation::evaluationCount($where);
        return json(['code' => 1, 'msg' => lang(''), 'data' => $list, 'count' => $count]);
    }
    
    //评价添加
    public function evaluationAdd(){
        //$store = new Store();
        $a_uid = Request::param('a_uid');
        $enable = Request::param('enable');
        $pid = Request::param('pid');
        $p_name = Request::param('p_name');
        $img_json = Request::param('img_json');
        $content = Request::param('content');
        $ptype = Request::param('ptype');//商品类型 1 商品 5 SKU
        $sku_id = Request::param('sku_id');//SKU ID
        $state = 2;
        $type = 2;
        
        
        
        
        //申请人
        $page = 1;
        $num = 1000;
        $where = [];
        $where[] = ['enable','=','1'];
        $where[] = ['type','=','2'];
        $where[] = ['pid','=',$pid];
        $orderTrialSelect = Evaluation::evaluationSelect($where);
        

        
        $uid_array = array();
        if(!empty($orderTrialSelect)){
            $orderTrialSelect = $orderTrialSelect->toArray();
            foreach ($orderTrialSelect as $value) {
                if(!in_array($value['uid'], $uid_array)){
                    array_push($uid_array,$value['uid']);
                }
                
            }
        }
        $text = implode(",", $uid_array);  
        
        //获取用户
        $where_u = [];
        if(!empty($text)){
            $where_u[] = ['uid','not in',$text];
        }
        $where_u[] = ['type','=',25];
        $where_u[] = ['enable','=',1];
        $userSelect = User::userSelect($where_u)->toArray();
        $user_arr = array();
        foreach ($userSelect as $value) {
            $user_arr[] = $value['uid'];
        }
        
        $uid = array_rand($user_arr);
        
        if(empty($enable)){
            $enable = 1;
        }
        if($enable == -1){
            $enable  = 0;
        }
        $time = time();
        $data = [
            'pid' => $pid,
            'p_name' => $p_name,
            'img_json' => $img_json,
            'sku_id' => $sku_id,
            'content' => $content,
            'uid' => $uid,
            'state' => $state,
            'type' => $type,
            'enable' => $enable,
            'addtime' => $time,
        ];

        Evaluation::evaluationAdd($data);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }
    
    //评价详情
    public function evaluationInfo(){
        $a_uid = Request::param('a_uid');

        $id = Request::param('id');
        $oid = Request::param('oid');
        $type = Request::param('type');
  
        $where = [];
   
        if(!empty($id)){
            $where[] = ['e.id','=',$id];
        }
        
        
        if(!empty($oid)){
            $where[] = ['e.oid','=',$oid];
        }
        
        if(!empty($where)){
            $info = Evaluation::evaluationInfo($where);
        }
        else{
            return json(['code' => 0, 'msg' => lang('请确认订单'), 'data' => '', 'count' => '' ]);
        }

        
        if(!empty($info)){
            $info = $info->toArray();

            $info["addtime"] = date( "Y-m-d H:i:s",$info["addtime"]);

            /*if(strpos($storeList['img'],"img.alicdn.com") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            elseif(strpos($storeList['img'],"http") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            else{

                $storeList["img"] =  Config::get('app.swimming_url').$storeList['img'];
            }*/
       
        }

        return json(['code' => 1, 'msg' => lang(''), 'data' => $info, 'count' => '' ]);
    }
    
    //评价修改
    public function evaluationEdit(){

        $a_uid = Request::param('a_uid');
        $id = Request::param('id');
        $enable = Request::param('enable');
        $state = Request::param('state');
//        $unit_weight_min = Request::param('unit_weight_min');
//        $unit_weight_max = Request::param('unit_weight_max');
//        $unit = Request::param('unit');
//        $price = Request::param('price');
//        $w_id = Request::param('w_id');
        
        $update = [];
        if(!empty($state)){
            $update['state'] = $state;
        }
        
//        if(!empty($unit_weight_min)){
//            $update['unit_weight_min'] = $unit_weight_min;
//        }
//        
//        if(!empty($unit_weight_max)){
//            $update['unit_weight_max'] = $unit_weight_max;
//        }
//        
//        if(!empty($unit)){
//            $update['unit'] = $unit;
//        }
//        
//        if(!empty($price)){
//            $update['price'] = $price;
//        }
//        
//        if(!empty($w_id)){
//            $update['w_id'] = $w_id;
//        }
//        
        if($enable==-1){
            $update['enable'] = 0;
        }else{
            $update['enable'] = 1;
        }


        Evaluation::evaluationEdit($id,$update);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }

}
